﻿@using Microsoft.AspNetCore.Identity
@using OrchardCore.Entities
@using OrchardCore.Settings
@using OrchardCore.Users
@using OrchardCore.Users.Models
@model OrchardCore.Users.ViewModels.LoginViewModel
@inject SignInManager<IUser> SignInManager
@inject ISiteService SiteService
@inject UserManager<IUser> UserManager;
@{
    ViewLayout = "Layout__Login";

    var userCanRegister = (await SiteService.GetSiteSettingsAsync()).As<RegistrationSettings>().UsersCanRegister == UserRegistrationType.AllowRegistration;
    var allowResetPassword = (await SiteService.GetSiteSettingsAsync()).As<ResetPasswordSettings>().AllowResetPassword;
    var loginProviders = (await SignInManager.GetExternalAuthenticationSchemesAsync()).ToList();
    var disableLocalLogin = (await SiteService.GetSiteSettingsAsync()).As<LoginSettings>().DisableLocalLogin;

    ViewData.ModelState.TryGetValue("UserName", out var userName);
    ViewData.ModelState.TryGetValue("Password", out var password);
}
<v-card class="px-4">
  <v-card-title>@T["Log In"]</v-card-title>
  @if (!disableLocalLogin)
  {
    <v-card-text>
      <form id="LoginForm" asp-controller="Account" asp-action="Login" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post">
        @if(ViewData.ModelState.ErrorCount > 0) 
        {
          <v-alert color="red" type="error">
            <ul>
              @foreach (var error in ViewData.ModelState.SelectMany(x => x.Value.Errors).Reverse())
              {
                <li>@error.ErrorMessage</li>
              }
            </ul>
          </v-alert>
        }
        <v-text-field id="UserName" name="UserName" label="@T["Email or username"]" value="@userName?.AttemptedValue" error-messages="@(userName?.Errors.FirstOrDefault()?.ErrorMessage)"></v-text-field>
        <v-text-field id="Password" type="password" name="Password" label="@T["Password"]" error-messages="@(password?.Errors.FirstOrDefault()?.ErrorMessage)"></v-text-field>
        <v-checkbox name="RememberMe" label="@T["Remember me"]" value="true"></v-checkbox>
        @if (allowResetPassword)
        {
          <a asp-controller="ResetPassword" asp-action="ForgotPassword" class="label-link">@T["Forgot your password?"]</a>
        }
      </form>
    </v-card-text>
    <v-card-actions>
      @await RenderSectionAsync("AfterLogin", required: false)
      <v-btn type="submit" form="LoginForm" x-large block color="success">@T["Log in"]</v-btn>
    </v-card-actions>
  }
  @if (loginProviders.Count > 0)
  {
    @if (!disableLocalLogin)
    {
      <div class="d-flex align-center">
        <v-divider></v-divider><span class="px-2">@T["or"]</span><v-divider></v-divider>
      </div>
    }
    <form asp-controller="Account" asp-action="ExternalLogin" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post">
      <v-card-actions>
        @foreach (var provider in loginProviders)
        {
          <v-btn type="submit" x-large block name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</v-btn>
        }
      </v-card-actions>
    </form>
  }
  @if (userCanRegister)
  {
    <div class="d-flex align-center">
      <v-divider></v-divider><span class="px-2">@T["or"]</span><v-divider></v-divider>
    </div>
    <v-card-actions>
      <v-btn href="@Url.Action("Register", "Registration", new { Area = "OrchardCore.Users", returnUrl=ViewData["ReturnUrl"] })" x-large block color="secondary">@T["Create an account"]</v-btn>
    </v-card-actions>
  }
</v-card>



